class ObserverList {
    constructor(){
        this.list=[]
    }
    count(){
        return this.list.length
    }
    get(index){
        return this.list[index]
    }
    indexOf(observer){
        return this.list.findIndex(ob=>ob===observer)
    }
    add(observer){
        this.list.push(observer)
    }
    removeAt(index){
        this.list.splice(index,1)
    }
}
class Observer{
    constructor(fn){
        this.update=fn
    }
}

class Subject{
    constructor(){
        this.observers=new ObserverList()
    }
    addObserver(observer){
        this.observers.add(observer)
    }
    removeObserver(observer){
        let index=this.observers.indexOf(observer)
        this.observers.remove(index)
    }
    notify(val){
        let length=this.observers.count()
        for(let i=0;i<length;i++){
            this.observers.get(i).update(val)
        }
    }    

}
const observer=new Observer((val)=>{
    console.log(val+'执行到了')
})
const subject=new Subject()

subject.addObserver(observer)
subject.notify()